# Copyright (C) 2008 Richard Boulton
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
r"""djapeclienttest.py: Framework for djape client unittests.

Unittests should just start with "from djapeclienttest import *", which will
provide a convenient environment for writing tests of the djape client.

"""
__docformat__ = "restructuredtext en"

# First, ensure that dependencies are on the path.
import sys
import os.path as osp
djapepath = osp.dirname(osp.dirname(osp.abspath(__file__)))
for ext in ['CherryPy-3.1.1', 'xappy']:
    sys.path.insert(0, osp.join(djapepath, 'ext', ext))
sys.path.insert(0, djapepath)

# Other imports
import djape.client
import os
import shutil
import tempfile
import unittest

class TestCase(unittest.TestCase):
    """Base class of djape client unittests.

    """
    def setUp(self):
        """Set up environment for a unittest.

        This should not normally be implemented in subclasses - instead,
        implement the pre_test() method, which is called by this method after
        performing the standard test setup process.

        """
        self.address = 'http://127.0.0.1:8080/search/'
        self.testdb = 'narwhal'

        c = djape.client.Client(self.address, self.testdb)
        try:
            c.deldb()
        except c.SearchClientError:
            pass

        self.pre_test()

    def tearDown(self):
        """Clean up after a unittest.

        This should not normally be implemented in subclasses - instead,
        implement the post_test() method, which is called by this method before
        performing the standard cleanup process.

        """
        self.post_test()

    def pre_test(self):
        """Prepare for a test.  This is called before a test is started, but
        after the standard setup process.

        This is intended to be overridden by subclasses when special setup is
        required for a test.

        """
        pass

    def post_test(self):
        """Cleanup after a test.  This is called after a test finishes, but
        before the standard cleanup process.

        This is intended to be overridden by subclasses when special cleanup is
        required for a test.

        """
        pass

    def assertOk(self, retval):
        """Assert that a search returned an "ok=1" status.

        """
        self.assertTrue(isinstance(retval, dict))
        self.assertEqual(retval.get('ok', None), 1)

    def assertNotOk(self, retval):
        """Assert that a search returned an "ok=0" status.

        """
        self.assertTrue(isinstance(retval, dict))
        self.assertEqual(retval.get('ok', None), 0)


main = unittest.main
